package com.ysyx.ysyx_driverorderquery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ysyx.ysyx_driverorderquery.dto.BaseDto;
import com.ysyx.ysyx_driverorderquery.mapper.OrderPojoMapper;
import com.ysyx.ysyx_driverorderquery.pojo.OrderPojo;
import com.ysyx.ysyx_driverorderquery.qo.TakeOrderQo;
import com.ysyx.ysyx_driverorderquery.service.OrderPojoService;
import org.redisson.api.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* @author 薛宇晖
* @description 针对表【order_inf(订单表)】的数据库操作Service实现
* @createDate 2023-04-17 15:07:58
*/
@Service
public class OrderPojoServiceImpl
    implements OrderPojoService {

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderPojoMapper orderPojoMapper;

    /*
     * @Description: (作用)根据订单id查看订单详情
     * @params: orderId
     * @return:
     * @Author: 薛宇晖
     * @create:2023/4/17
     */
    @Override
    public BaseDto queryOrderinfo(String orderId) throws ParseException {

        List<TakeOrderQo> qos = new ArrayList<>();

        RMap<Object, Object> map = redissonClient.getMap("orderinfo:" + orderId);

        //去获取司机的位置信息
        //去获取司机的位置信息
        RGeo<String> geo = redissonClient.getGeo("order:track:" + orderId);
        //获取到司机id
//        String name = "";
//        for (String o : geo) {
//            name = o;
//        }
        Object[] objects = geo.toArray();
        if (objects.length==0) return new BaseDto(101,"没有订单数据",null);
        String name  = (String) objects[objects.length-1];


        //获取到一个map    k 时间戳     v经纬度
        Map<String, GeoPosition> pos = geo.pos(name);
        double latitude = pos.get(name).getLatitude();
        double longitude = pos.get(name).getLongitude();



        TakeOrderQo o = new TakeOrderQo();
        o.setDriverLongitude(longitude);
        o.setDriverLatitude(latitude);

        if (map.size()>0){

            o.setOrderId((String) map.get("orderId"));
            o.setPassengerId(Integer.valueOf((String) map.get("passengerId")));
            o.setOrderStatus(Integer.valueOf((String) map.get("orderStatus")));
            o.setOrderCost((Double.parseDouble((String) map.get("orderCost"))));
            o.setOrderMileage((Double.parseDouble((String) map.get("orderMileage"))) );
            o.setDriverId((Integer.valueOf((String) map.get("driverId"))));
            o.setStartName((String) map.get("startName"));
            o.setEndName((String) map.get("endName"));
            o.setDriverTel((String) map.get("driverTel"));
            o.setPassengerTel((String) map.get("passengerTel"));
//            (Date) orderMap.get("orderStatustime")
//            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-03 10:59:27")


            o.setOrderStatustime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
            o.setOrderStatus((Integer.valueOf((String) map.get("orderStatus"))));
            qos.add(o);
        }


        //获取到对象


        if (qos.size()==0){
            //没有获取到
//            去查数据库
            TakeOrderQo takeOrderQo = new TakeOrderQo();
            takeOrderQo.setOrderId(orderId);
            TakeOrderQo takeOrderQo1 = orderPojoMapper.queryOrderinfoByorderid(takeOrderQo);
            if (takeOrderQo1==null){
                return new BaseDto(101,"订单数据丢失",null);
            }
            qos.add(takeOrderQo1);

        }

        return new BaseDto(201,"获取到订单详细信息",qos);
    }

    @Override
    public BaseDto queryDoingOrder(int driverId) throws ParseException {
        RSet<Object> set = redissonClient.getSet("working:driver");
        Boolean iswork = false;
        for (Object o : set) {
            if (o.equals(String.valueOf(driverId))){
                iswork = true;
                break;
            }


        }
        System.err.println(iswork);

        if (iswork){
            //如果司机在工作状态
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select().orderByDesc("order_statustime");
            queryWrapper.eq("driver_id",driverId);
            List<OrderPojo> list = orderPojoMapper.selectList(queryWrapper);
            OrderPojo orderPojo = list.get(0);
            System.err.println("订单号:"+orderPojo.getOrderId());

            RMap<Object, Object> map = redissonClient.getMap("orderinfo:" + orderPojo.getOrderId());
            List<TakeOrderQo> qos = new ArrayList<>();
            RGeo<String> geo = redissonClient.getGeo("order:track:"+orderPojo.getOrderId());
            System.out.println(geo.size()+"尺寸");
            //获取到司机id
//            String name = "";
//            for (String o : geo) {
//                name = o;
//            }

            Object[] objects = geo.toArray();
            if (objects.length==0){
             return new BaseDto(201,"没有进行中的订单",null);
            }
            String name = (String) objects[objects.length-1];



            System.out.println(name+"_杰荣肉");
            //获取到一个map    k 时间戳     v经纬度
            Map<String, GeoPosition> pos = geo.pos(name);
            double latitude = pos.get(name).getLatitude();
            double longitude = pos.get(name).getLongitude();



            TakeOrderQo o = new TakeOrderQo();
            o.setDriverLongitude(longitude);
            o.setDriverLatitude(latitude);

            if (map.size()>0){

                o.setOrderId((String) map.get("orderId"));
                o.setPassengerId(Integer.valueOf((String) map.get("passengerId")));
                o.setOrderStatus(Integer.valueOf((String) map.get("orderStatus")));
                o.setOrderCost((Double.parseDouble((String) map.get("orderCost"))));
                o.setOrderMileage((Double.parseDouble((String) map.get("orderMileage"))) );
                o.setDriverId((Integer.valueOf((String) map.get("driverId"))));
                o.setStartName((String) map.get("startName"));
                o.setEndName((String) map.get("endName"));
                o.setDriverTel((String) map.get("driverTel"));
                o.setPassengerTel((String) map.get("passengerTel"));

                o.setStartLatitude(Double.parseDouble((String) map.get("startLatitude")));
                o.setStartLongitude(Double.parseDouble((String) map.get("startLongitude")));
                o.setEndLongitude(Double.parseDouble((String) map.get("endLongitude")));
                o.setEndLatitude(Double.parseDouble((String) map.get("endLatitude")));
//            (Date) orderMap.get("orderStatustime")
//            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-03 10:59:27")


                o.setOrderStatustime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
                o.setOrderStatustime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
//                o.setOrderStatustime((String)map.get("orderStatustime"));
                System.err.println("xx");
                System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
                o.setOrderStatus((Integer.valueOf((String) map.get("orderStatus"))));
                qos.add(o);
                return new BaseDto(101,"有在进行中的订单",qos);
            }

        }




        return new BaseDto(201,"没有进行中的订单",null);
    }
}




